﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Globalization;
public partial class Users_AddMoney : System.Web.UI.Page
{
    private string _bank;//银行列表
    public string Bank
    {
        set { _bank = value; }
        get { return _bank; }
    }
    private string _payMethod;//银行列表
    public string PayMethod
    {
        set { _payMethod = value; }
        get { return _payMethod; }
    }
    public decimal Limit
    {
        get { return decimal.Parse(new BLL.SYSPARAM().GetModel("016").SP_PARAM19); }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       // Master.Sign = "1";
        Init_PayMethod();
        Init_Bank();
        if (!IsPostBack)
        {
            Page.Title = BasePage.WebTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                Response.Redirect("Login.aspx?from=" + Request.Url.ToString());
            }
            else
            {
                BLL.V_CUSTOMER bll_customer = new BLL.V_CUSTOMER();
                lblUserCode.Text = bll_customer.GetModel(BasePage.GetValue(0)).DM;
            }
            
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSThemes();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ConfigurationManager.AppSettings["ISPay"].ToString() == "1")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('模拟版本,不提供此功能!')", true);
        }
        else
        {
            string type = Hid_Type.Value; //网上支付类型

            //Guid _ugid = Guid.NewGuid();// ;//new BLL.REMITTANCE().getMaxId() + 1  ;// 323444123 ; 
            //new BLL.REMITTANCE().getMaxId() + 1
            string orderid = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            string money = moneytext.Text; //"1234"; // Hidden1.Value; //值 
            try
            {
                decimal i = Convert.ToDecimal(money);
            }
            catch
            {
                Response.Write("<script>alert('金额输入值不正确');history.go(-1); </script>");
                Response.End();
            }
            if (money == "") Response.Write("<script>alert('金额输入值不正确');history.go(-1); </script>");

            string _usercode = "jsycywm@winner.com";

            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                DataSet ds = new BLL.V_CUSTOMER().UserCenterIndexInfo(BasePage.GetValue(0));
                if (ds != null)
                {
                    _usercode = ds.Tables[0].Rows[0]["DM"].ToString();
                }
            }
            orderid = _usercode + "_" + orderid;

            decimal limit = decimal.Parse(new BLL.SYSPARAM().GetModel("016").SP_PARAM19);
            decimal aa = decimal.Parse(money);
            Model.PAYTYPE pt = new BLL.PAYTYPE().GetModel(type);
            decimal fl = 0.00M;
            decimal.TryParse(pt.PA_FIELD2,out fl);
            if (pt.PA_FIELD2.Length > 0)
            {
                aa = aa - aa * fl;
            }
            if (aa < limit)
            {
                Response.Write("<script>alert('扣除费率后，金额不能小于最低充值金额" + limit.ToString() + "元(当前费率是" + (fl*100).ToString("0.00") + "%)');history.go(-1); </script>");
                Response.End();
            }

            if (pt.PT_NAME.Contains("支付宝"))
            {
                AliPay.ToPay(orderid.ToString(), money, true, _usercode);
            }
            else if (pt.PT_NAME.Contains("快钱"))
            {
                Session["OrderId"] = orderid;
                Session["v_amount"] = money;
                Response.Redirect("../Pay/99bill/nSend.aspx");
            }
            else
            {
                Response.Write("<script>alert('没有提供此接口');history.go(-1); </script>");
                Response.End();
            }
           /* switch (type)
            {
                case "300": //支付宝                
                    AliPay.ToPay(orderid.ToString(), money, true, _usercode);
                    break;
                //case "301": //网银在线                
                //    Session["OrderId"] = orderid;
                //    Session["v_amount"] = money;
                //    Response.Redirect("../Pay/chinabank/Send.aspx");
                //    break;
                case "302": //快钱                
                    Session["OrderId"] = orderid;
                    Session["v_amount"] = money;
                    Response.Redirect("../Pay/99bill/nSend.aspx");
                    break;
                default:
                    Response.Write("<script>alert('没有提供此接口');history.go(-1); </script>");
                    Response.End();
                    break;
            }*/
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (hdPayCode.Value.Length < 1)
        {
            Response.Write("<script>alert('请选择开户行');history.go(-1); </script>");
            Response.End();
           // BLL.Common.Alert(this, "请选择开户行");
            //return; 
        }

        //检查转账日期不能为空
        if (txtDate.Text.Trim().Length == 0)
        {
            Response.Write("<script>alert('转账日期不能为空');history.go(-1); </script>");
            Response.End();
        }
        if (txtDate.Text.Trim().Length > 0)
        {
            DateTime date = DateTime.Now;
            if (!DateTime.TryParse(txtDate.Text.Trim(), out date))
            {
                Response.Write("<script>alert('请输入正确的转账日期');history.go(-1); </script>");
                Response.End();
            }
        }
        if (hdHaveDeal.Value=="1"&&dealNo.Text.Trim().Length < 1)
        {
            Response.Write("<script>alert('交易号不能为空');history.go(-1); </script>");
            Response.End();
           // BLL.Common.Alert(this, "交易号不能为空");
            //return;
        }

        //检查交易号是否有重复

        if (dealNo.Text.Trim().Length > 0)
        {
            if (new BLL.REMITTANCE().CheckRepeat(dealNo.Text.Trim(), hdPayCode.Value))
            {
                Response.Write("<script>alert('交易号重复');history.go(-1); </script>");
                Response.End();
                //BLL.Common.Alert(this,"交易号重复");
                //return;
            }
        }
        if (JE.Text.Trim().Length < 1)
        {
            Response.Write("<script>alert('金额不能为空');history.go(-1); </script>");
            Response.End();
            //BLL.Common.Alert(this, "金额不能为空");
            //return;
        }
        else
        {
            try
            {
                decimal limit = decimal.Parse(new BLL.SYSPARAM().GetModel("016").SP_PARAM19);
                decimal aa = decimal.Parse(JE.Text.Trim());
                if (aa < limit)
                {
                    Response.Write("<script>alert('金额必须大于最低充值金额" + limit.ToString() + "元');history.go(-1); </script>");
                    Response.End();
                }
            }
            catch
            {
                BLL.Common.Alert(this, "金额只能为数字");
                return;
            }
        }
        Model.REMITTANCE remittance = new Model.REMITTANCE();
        remittance.R_UserCode = BasePage.GetValue(0);//用户帐号
        remittance.R_PostMethod = hdPayCode.Value;//支付方式代码
        remittance.R_NO = dealNo.Text.Trim();
        remittance.R_Amt = decimal.Parse(JE.Text.Trim());
        remittance.R_AddTime = DateTime.Parse(txtDate.Text.Trim());
        remittance.R_Maker = BasePage.GetValue(0);
        remittance.R_DST = drpType.SelectedValue;
        remittance.R_Valid = 0;
        remittance.R_Validor = "";
        remittance.BYZD1 = hdBankName.Value;//银行名称
        remittance.BYZD2 = hdAccount.Value;//户主帐号
        remittance.R_Note = "";
        int ID = new BLL.REMITTANCE().Add(remittance);

        if (ID > 0)
        {
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('转账成功')", true);
            Response.Write("<script>alert('转账成功');history.go(-1); </script>");
            Response.End();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('操作失败')", true);
            Response.Write("<script>alert('操作失败');history.go(-1); </script>");
            Response.End();
        }
        return;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    }

    #region  初始化支付方式
    private void Init_PayMethod()
    {
        StringBuilder Sb = new StringBuilder();
        string where = " and PT_ONLINE=1 and PT_Bank=0";
        BLL.PAYTYPE paytype = new BLL.PAYTYPE();
        DataSet ds = paytype.GetPatTypeByWhere(where);
        if (ds.Tables.Count > 0)
        {
            Sb.Append("<table id=\"TypeList\" width=\"100%\">");
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Sb.Append("<tr><td>");
                Sb.Append("<table><tr>");
                Sb.Append("<td><input name=\"radiobutton\" type=\"radio\" value=\"" + row["PT_CODE"] + "\" onclick=\"ChoosePayMethod(this)\" /></td>");
                Sb.Append("<td><span><img src=\"../images/" + row["PA_LOGO"] + "\"></span></td>");
                Sb.Append("</td>");
                Sb.Append("<td>");
                Sb.Append("<span>" + row["PT_NAME"] + "</span>");
                Sb.Append("</td>");
                Sb.Append("<td>(帐号：");
                Sb.Append("<span>" + row["PA_ACCOUNT"] + "</span>");
                Sb.Append(",户主：" + row["PT_TEXT1"]);
                Sb.Append(")</td></tr></table></tr>");
            }

            Sb.Append("</table>");
            PayMethod = Sb.ToString();
        }
    }
    #endregion

    #region 初始化银行
    private void Init_Bank()
    {
        StringBuilder Sb = new StringBuilder();
        string where = " and PT_Bank=1 and PT_ONLINE=1";
        BLL.PAYTYPE paytype = new BLL.PAYTYPE();
        DataSet ds = paytype.GetPatTypeByWhere(where);
        if (ds.Tables.Count > 0)
        {
            Sb.Append("<table id=\"BankList\" width=\"100%\">");
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Sb.Append("<tr><td>");
                Sb.Append("<table><tr><td>");
                Sb.Append("<input name=\"radiobank\" type=\"radio\" value=\"" + row["PT_CODE"] + "\" title=\""+row["PT_HAVEDEAL"]+ "\"  onclick=\"ChooseBank(this)\" />");
                Sb.Append("<input id=\"" + row["PT_CODE"] + "\" value=\"" + row["PT_NAME"] + "\" name=\"" + row["PA_ACCOUNT"] + "\" type=\"hidden\">");
                Sb.Append("</td><td>");
                Sb.Append("<span>" + row["PT_NAME"] + "</span>");
                Sb.Append("</td></tr></table>");
                Sb.Append("</td>");
                Sb.Append("<td>帐号：");
                Sb.Append("<span>" + row["PA_ACCOUNT"] + "</span>");
                Sb.Append("</td></tr>");
            }
            Sb.Append("</table>");
            Bank = Sb.ToString();
        }
    }
    #endregion
}
